﻿using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace Sizebox.CharacterEditor
{
	// Token: 0x02000259 RID: 601
	public abstract class BasePresetsView : BaseView
	{
		// Token: 0x06000D18 RID: 3352 RVA: 0x00068E20 File Offset: 0x00067020
		protected override void Awake()
		{
			base.Awake();
			this.loadButton.onClick.AddListener(new UnityAction(this.ExecuteLoad));
			this.saveButton.onClick.AddListener(new UnityAction(this.OnSave));
			this.deleteButton.onClick.AddListener(new UnityAction(this.OnDeleteFile));
		}

		// Token: 0x06000D19 RID: 3353 RVA: 0x00068E88 File Offset: 0x00067088
		protected override void OnEnable()
		{
			if (!this.handleManager.TargetEditor)
			{
				return;
			}
			this.folderPath = this.handleManager.TargetEditor.FolderPath + Path.DirectorySeparatorChar + this.presetFolder;
			this.LoadFolderEntries(this.folderPath);
		}

		// Token: 0x06000D1A RID: 3354 RVA: 0x00068EE2 File Offset: 0x000670E2
		public void SetTarget(PresetsFileEntryGui target)
		{
			if (this.targetFile)
			{
				this.targetFile.Selected = false;
			}
			this.targetFile = target;
			target.Selected = true;
		}

		// Token: 0x06000D1B RID: 3355 RVA: 0x00068F0E File Offset: 0x0006710E
		protected void EnsureFolder(string folderPath)
		{
			if (!Directory.Exists(folderPath))
			{
				Directory.CreateDirectory(folderPath);
			}
		}

		// Token: 0x06000D1C RID: 3356 RVA: 0x00068F24 File Offset: 0x00067124
		private void LoadFolderEntries(string folderPath)
		{
			this.RemoveFileEntries();
			this.EnsureFolder(folderPath);
			string[] files = Directory.GetFiles(folderPath);
			foreach (string filePath in files)
			{
				this.LoadFileEntry(filePath);
			}
		}

		// Token: 0x06000D1D RID: 3357 RVA: 0x00068F68 File Offset: 0x00067168
		private void LoadFileEntry(string filePath)
		{
			if (Path.GetExtension(filePath) == this.PRESET_EXTENSION && File.Exists(filePath))
			{
				PresetsFileEntryGui presetsFileEntryGui = UnityEngine.Object.Instantiate<PresetsFileEntryGui>(this.fileEntryPrefab, this.fileEntryParent);
				this.fileEntries.Add(presetsFileEntryGui);
				presetsFileEntryGui.Initialize(filePath);
			}
		}

		// Token: 0x06000D1E RID: 3358 RVA: 0x00068FBB File Offset: 0x000671BB
		private void OnDeleteFile()
		{
			if (this.targetFile)
			{
				this.confirmationDialog.Open(new UnityAction(this.DeleteTargetFile), null, this.DELETE_CONFIRMATION_MESSAGE);
			}
		}

		// Token: 0x06000D1F RID: 3359 RVA: 0x00068FEB File Offset: 0x000671EB
		private void DeleteTargetFile()
		{
			if (this.targetFile)
			{
				this.DeleteFile(this.targetFile.FilePath);
			}
			this.LoadFolderEntries(this.folderPath);
		}

		// Token: 0x06000D20 RID: 3360 RVA: 0x0006901A File Offset: 0x0006721A
		private void DeleteFile(string path)
		{
			if (File.Exists(path))
			{
				File.Delete(path);
			}
		}

		// Token: 0x06000D21 RID: 3361 RVA: 0x00069030 File Offset: 0x00067230
		private void RemoveFileEntries()
		{
			foreach (MonoBehaviour monoBehaviour in this.fileEntries)
			{
				UnityEngine.Object.Destroy(monoBehaviour.gameObject);
			}
			this.fileEntries.Clear();
		}

		// Token: 0x06000D22 RID: 3362 RVA: 0x0006909C File Offset: 0x0006729C
		private void OnSave()
		{
			string name = null;
			if (this.targetFile)
			{
				name = Path.GetFileNameWithoutExtension(this.targetFile.FilePath);
			}
			this.saveDialog.Open(this.folderPath, new UnityAction<string>(this.ExecuteSave), name);
		}

		// Token: 0x06000D23 RID: 3363 RVA: 0x000690EA File Offset: 0x000672EA
		private void ExecuteSave(string fileName)
		{
			this.SaveFile(fileName);
			this.LoadFolderEntries(this.folderPath);
		}

		// Token: 0x06000D24 RID: 3364 RVA: 0x000690FF File Offset: 0x000672FF
		private void ExecuteLoad()
		{
			if (!this.handleManager.TargetEditor || !this.targetFile)
			{
				return;
			}
			this.LoadFile(this.targetFile.FilePath);
		}

		// Token: 0x06000D25 RID: 3365 RVA: 0x00069138 File Offset: 0x00067338
		protected void OnLoadFail()
		{
			this.messageDialog.Open(this.LOAD_FAIL_MESSAGE);
		}

		// Token: 0x06000D26 RID: 3366
		protected abstract void SaveFile(string fileName);

		// Token: 0x06000D27 RID: 3367
		protected abstract void LoadFile(string filePath);

		// Token: 0x04001220 RID: 4640
		[Header("Prefabs")]
		[SerializeField]
		protected PresetsFileEntryGui fileEntryPrefab;

		// Token: 0x04001221 RID: 4641
		[Header("References")]
		[SerializeField]
		protected HandleManager handleManager;

		// Token: 0x04001222 RID: 4642
		[SerializeField]
		protected RectTransform fileEntryParent;

		// Token: 0x04001223 RID: 4643
		[SerializeField]
		protected SaveDialog saveDialog;

		// Token: 0x04001224 RID: 4644
		[SerializeField]
		protected ConfirmationDialog confirmationDialog;

		// Token: 0x04001225 RID: 4645
		[SerializeField]
		protected MessageDialog messageDialog;

		// Token: 0x04001226 RID: 4646
		[Header("Buttons")]
		[SerializeField]
		protected Button loadButton;

		// Token: 0x04001227 RID: 4647
		[SerializeField]
		protected Button saveButton;

		// Token: 0x04001228 RID: 4648
		[SerializeField]
		protected Button deleteButton;

		// Token: 0x04001229 RID: 4649
		[SerializeField]
		[Header("Preset Settings")]
		protected string presetFolder = string.Empty;

		// Token: 0x0400122A RID: 4650
		[SerializeField]
		protected string PRESET_EXTENSION = ".preset";

		// Token: 0x0400122B RID: 4651
		[SerializeField]
		protected string LOAD_FAIL_MESSAGE = "The selected preset could not be loaded.";

		// Token: 0x0400122C RID: 4652
		[SerializeField]
		protected string DELETE_CONFIRMATION_MESSAGE = "Are you sure you want to delete this preset?";

		// Token: 0x0400122D RID: 4653
		protected List<PresetsFileEntryGui> fileEntries = new List<PresetsFileEntryGui>();

		// Token: 0x0400122E RID: 4654
		protected string folderPath;

		// Token: 0x0400122F RID: 4655
		protected PresetsFileEntryGui targetFile;
	}
}
